package com.atguigu.gulimallcart.vo;

import lombok.ToString;

import java.math.BigDecimal;
import java.util.List;

/**
 * 购物车Vo
 */
@ToString
public class Cart {
    private List<CartItem> items;
    private Integer countNum;
    private Integer countType;
    private BigDecimal reduce=new BigDecimal("0");
    private BigDecimal totalAmount;

    public List<CartItem> getItems() {
        return items;
    }

    public void setItems(List<CartItem> items) {
        this.items = items;
    }

    /**
     * 计算总共有多少项
     * @return
     */
    public Integer getCountNum() {
        int count = 0;
        if (this.items != null && this.items.size() > 0) {
            for (CartItem item : items) {
                count += item.getCount();
            }
        }
        return count;
    }

    public void setCountNum(Integer countNum) {
        this.countNum = countNum;
    }

    /**
     * 计算商品的分类
     * @return
     */
    public Integer getCountType() {
        int count = 0;
        if (this.items != null && this.items.size() > 0) {
            for (CartItem item : items) {
                count += 1;
            }
        }
        return count;
    }

    public void setCountType(Integer countType) {
        this.countType = countType;
    }

    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }

    /**
     * 商品的总计金额
     * @return
     */
    public BigDecimal getTotalAmount() {
        BigDecimal totalPrice = new BigDecimal(0);
        if (this.items != null && this.items.size() > 0) {
            for (CartItem item : items) {
                totalPrice = totalPrice.add(new BigDecimal(item.getTotalPrice() + ""));
            }
        }
        //减免的金额
        BigDecimal subtract = totalPrice.subtract(new BigDecimal(String.valueOf(getReduce())));
        return subtract;
    }

    public void setTotalAmount(BigDecimal totalAmount) {
        this.totalAmount = totalAmount;
    }
}

